package com.droid.lib.controls.demo.view;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.droid.lib.controls.demo.R;
import com.droid.lib.controls.widget.RippleView;
import com.droid.lib.controls.demo.widget.tree.AndroidTreeView;
import com.droid.lib.controls.demo.widget.tree.TreeNode;
import com.droid.lib.controls.demo.widget.tree.demo.HeaderHolder;
import com.droid.lib.controls.demo.widget.tree.demo.IconTreeItemHolder;
import com.droid.lib.controls.demo.widget.tree.demo.PlaceHolderHolder;
import com.droid.lib.controls.demo.widget.tree.demo.ProfileHolder;
import com.droid.lib.controls.demo.widget.tree.demo.ProfileHolders;
import com.droid.lib.controls.demo.widget.tree.demo.SelectableHeaderHolder;
import com.droid.lib.controls.demo.widget.tree.demo.SelectableItemHolder;
import com.droid.lib.controls.demo.widget.tree.demo.SocialViewHolder;
import com.droid.lib.controls.demo.widget.tree.demo.TreeChildHolder;
import com.droid.lib.controls.demo.widget.tree.demo.TreeItemHolder;

/**
 * 树形
 * Created by Administrator on 2016/9/27.
 */
public class TreeFragment extends Fragment implements View.OnClickListener {
    private Context mContext;
    private RippleView mrvSet, mrvAdd, mrvRemove, mrvGetNum;
    private ViewGroup containerView;
    private AndroidTreeView tView;
    private boolean selectionModeEnabled = false;
    private RippleView rv_taball, rv_custom_holder_example, rv_selectable_nodes;
    private View view;
    private Bundle savedInstanceState;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mContext = getActivity();
        this.savedInstanceState = savedInstanceState;
        view = inflater.inflate(R.layout.fragment_tree, null);
        rv_taball = (RippleView) view.findViewById(R.id.rv_taball);
        rv_taball.setOnClickListener(this);
        rv_custom_holder_example = (RippleView) view.findViewById(R.id.rv_custom_holder_example);
        rv_custom_holder_example.setOnClickListener(this);
        rv_selectable_nodes = (RippleView) view.findViewById(R.id.rv_selectable_nodes);
        rv_selectable_nodes.setOnClickListener(this);
        mrvSet = (RippleView) view.findViewById(R.id.rv_set);
        mrvSet.setOnClickListener(this);
        mrvAdd = (RippleView) view.findViewById(R.id.rv_add);
        mrvAdd.setOnClickListener(this);
        mrvRemove = (RippleView) view.findViewById(R.id.rv_remove);
        mrvRemove.setOnClickListener(this);
        mrvGetNum = (RippleView) view.findViewById(R.id.rv_get_num);
        mrvGetNum.setOnClickListener(this);
        containerView = (ViewGroup) view.findViewById(R.id.container);


        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rv_set:
                selectionModeEnabled = !selectionModeEnabled;
                tView.setSelectionModeEnabled(selectionModeEnabled);

                break;
            case R.id.rv_add:
                tView.selectAll(true);
                break;
            case R.id.rv_remove:
                tView.deselectAll();
                break;
            case R.id.rv_get_num:
                if (!selectionModeEnabled) {
                    Toast.makeText(getActivity(), "Enable selection mode first", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getActivity(), tView.getSelected().size() + " selected", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.rv_taball:
                if (containerView.getChildCount() > 0) {
                    containerView.removeAllViews();
                }
                view.findViewById(R.id.status_bar).setVisibility(View.GONE);
                TreeNode root1 = TreeNode.root();
                TreeNode 王庆才 = new TreeNode(new TreeItemHolder.TreeItem(getActivity().getResources().getDrawable(R.drawable.rb_icon_living_conditions_pressed), "王庆才")).setViewHolder(new ProfileHolders(getActivity()));
                TreeNode 王星星 = new TreeNode(new TreeItemHolder.TreeItem(getActivity().getResources().getDrawable(R.drawable.rb_icon_living_conditions_pressed), "王星星")).setViewHolder(new ProfileHolders(getActivity()));
                王庆才.setSelectable(false);
                王星星.setSelectable(true);
                TreeNode 王淑静 = new TreeNode(new TreeItemHolder.TreeItem(getActivity().getResources().getDrawable(R.drawable.rb_icon_business_project_checked), "王淑静")).setViewHolder(new TreeItemHolder(getActivity()));
                TreeNode 王为娟 = new TreeNode(new TreeItemHolder.TreeItem(getActivity().getResources().getDrawable(R.drawable.rb_icon_business_project_checked), "王为娟")).setViewHolder(new TreeItemHolder(getActivity()));
                王庆才.addChild(王淑静);
                王星星.addChild(王为娟);
                王淑静.setSelected(true);
                王为娟.setSelectable(true);
                TreeNode file1 = new TreeNode("孙敏").setViewHolder(new TreeChildHolder(getActivity()));
                王淑静.addChild(file1);
                TreeNode file2 = new TreeNode("刘有").setViewHolder(new TreeChildHolder(getActivity()));
                王淑静.addChild(file2);
                root1.addChildren(王庆才, 王星星);
                tView = new AndroidTreeView(getActivity(), root1);
                tView.setDefaultAnimation(true);
                containerView.addView(tView.getView());
                break;
            case R.id.rv_custom_holder_example:
                if (containerView.getChildCount() > 0) {
                    containerView.removeAllViews();
                }
                view.findViewById(R.id.status_bar).setVisibility(View.GONE);
                final TreeNode root = TreeNode.root();

                TreeNode myProfile = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_person, "My Profile")).setViewHolder(new ProfileHolder(getActivity()));
                TreeNode bruce = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_person, "Bruce Wayne")).setViewHolder(new ProfileHolder(getActivity()));
                TreeNode clark = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_person, "Clark Kent")).setViewHolder(new ProfileHolder(getActivity()));
                TreeNode barry = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_person, "Barry Allen")).setViewHolder(new ProfileHolder(getActivity()));
                addProfileData(myProfile);
                addProfileData(clark);
                addProfileData(bruce);
                addProfileData(barry);
                root.addChildren(myProfile, bruce, barry, clark);

                tView = new AndroidTreeView(getActivity(), root);
                tView.setDefaultAnimation(true);
                tView.setDefaultContainerStyle(R.style.TreeNodeStyleDivided, true);
                containerView.addView(tView.getView());

                if (savedInstanceState != null) {
                    String state = savedInstanceState.getString("tState");
                    if (!TextUtils.isEmpty(state)) {
                        tView.restoreState(state);
                    }
                }
                break;
            case R.id.rv_selectable_nodes:
                if (containerView.getChildCount() > 0) {
                    containerView.removeAllViews();
                }
                TreeNode root2 = TreeNode.root();
                view.findViewById(R.id.status_bar).setVisibility(View.VISIBLE);
                TreeNode s1 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_sd_storage, "Storage1")).setViewHolder(new ProfileHolder(getActivity()));
                TreeNode s2 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_sd_storage, "Storage2")).setViewHolder(new ProfileHolder(getActivity()));
                s1.setSelectable(false);
                s2.setSelectable(false);

                TreeNode folder1 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_folder, "Folder 1")).setViewHolder(new SelectableHeaderHolder(getActivity()));
                TreeNode folder2 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_folder, "Folder 2")).setViewHolder(new SelectableHeaderHolder(getActivity()));
                TreeNode folder3 = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_folder, "Folder 3")).setViewHolder(new SelectableHeaderHolder(getActivity()));

                fillFolder(folder1);
                fillFolder(folder2);
                fillFolder(folder3);

                s1.addChildren(folder1, folder2);
                s2.addChildren(folder3);

                root2.addChildren(s1, s2);

                tView = new AndroidTreeView(getActivity(), root2);
                tView.setDefaultAnimation(true);
                containerView.addView(tView.getView());
                break;

        }
    }

    private void addProfileData(TreeNode profile) {
        TreeNode socialNetworks = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_people, "Social")).setViewHolder(new HeaderHolder(getActivity()));
        TreeNode places = new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_place, "Places")).setViewHolder(new HeaderHolder(getActivity()));

        TreeNode facebook = new TreeNode(new SocialViewHolder.SocialItem(R.string.ic_post_facebook)).setViewHolder(new SocialViewHolder(getActivity()));
        TreeNode linkedin = new TreeNode(new SocialViewHolder.SocialItem(R.string.ic_post_linkedin)).setViewHolder(new SocialViewHolder(getActivity()));
        TreeNode google = new TreeNode(new SocialViewHolder.SocialItem(R.string.ic_post_gplus)).setViewHolder(new SocialViewHolder(getActivity()));
        TreeNode twitter = new TreeNode(new SocialViewHolder.SocialItem(R.string.ic_post_twitter)).setViewHolder(new SocialViewHolder(getActivity()));

        TreeNode lake = new TreeNode(new PlaceHolderHolder.PlaceItem("A rose garden")).setViewHolder(new PlaceHolderHolder(getActivity()));
        TreeNode mountains = new TreeNode(new PlaceHolderHolder.PlaceItem("The white house")).setViewHolder(new PlaceHolderHolder(getActivity()));

        places.addChildren(lake, mountains);
        socialNetworks.addChildren(facebook, google, twitter, linkedin);
        profile.addChildren(socialNetworks, places);
    }

    private void fillFolder(TreeNode folder) {
        TreeNode file1 = new TreeNode("File1").setViewHolder(new SelectableItemHolder(getActivity()));
        TreeNode file2 = new TreeNode("File2").setViewHolder(new SelectableItemHolder(getActivity()));
        TreeNode file3 = new TreeNode("File3").setViewHolder(new SelectableItemHolder(getActivity()));
        folder.addChildren(file1, file2, file3);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString("tState", tView.getSaveState());
    }
}
